<?php

use yii\helpers\Url;
use yii\helpers\Html;
use website\helpers\Render;
use common\models\Design;
use common\models\Recruit;
use common\models\Category;

$this->addCrumbs('招聘管理');
$this->title = $recruit ? '修改招聘信息' : '发布招聘';
?>

<div class="contenter">
    <?= $this->render('/user/navigator', ['active' => 'recruit']) ?>

    <div id="user-contenter">
        <div class="box-shadow" id="sub-contenter">
            <div class="data-title"><span class="h3"><?= $this->title ?></span></div>
            <div class="data-content flyer-form">
                <form class="mt-20px" id="flyer-create" onsubmit="return false;">
                    <div class="form-item checker">
                        <div class="input-title">标题：</div>
                        <div class="input-block"><input class="flyer-input" name="title" type="text" value="<?= Render::value($recruit, 'title') ?>"></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">选择平台：</div>
                        <div class="input-block flyer-choise-conter moderate red-choise"><?= Render::radio('platforms', Design::$platformSelector, Render::value($recruit, 'platforms')) ?></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">选择类目：</div>
                        <div class="input-block flyer-choise-conter moderate red-choise"><?= Render::radio('categories', $categories, Render::value($recruit, 'categories'), [], ['value' => 'id']) ?></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">设计类型：</div>
                        <div class="input-block flyer-choise-conter moderate red-choise"><?= Render::checkbox('models', $models, Render::value($recruit, 'models'), [], ['value' => 'id']) ?></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">雇佣类型：</div>
                        <div class="input-inline checker gender clear">
                            <input name="type" value="1" type="radio" flyer="radio" title="在线全职">
                            <input name="type" value="2" type="radio" flyer="radio" title="在线定制" checked>
                        </div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">您的预算：</div>
                        <div class="input-block flyer-choise-conter moderate red-choise"><?= Render::radio('budget', Recruit::$budgetSelector, Render::value($recruit, 'budget')) ?></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">设计风格：</div>
                        <div class="input-block flyer-choise-conter moderate red-choise"><?= Render::checkbox('styles', Design::$styleSelector, Render::value($recruit, 'styles')) ?></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">我的店铺：</div>
                        <div class="input-block"><input class="flyer-input" name="website" type="text" value="<?= Render::value($recruit, 'website') ?>" placeholder="https://shop60559652.taobao.com/?spm=a1z10.1-c-s.0.0.d92b6f8fyRXKHR"></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">参考店铺：</div>
                        <div class="input-block"><input class="flyer-input" name="refer_to" type="text" value="<?= Render::value($recruit, 'refer_to') ?>" placeholder="多个参考店铺之间以`;`隔开"></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">补充说明：</div>
                        <div class="input-block"><textarea class="flyer-textarea" name="remark"><?= Render::value($recruit, 'remark') ?></textarea></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">是否上架：</div>
                        <div class="input-inline checker gender clear">
                            <input name="online" value="1" type="radio" flyer="radio" title="发布" checked>
                            <input name="online" value="0" type="radio" flyer="radio" title="暂不发布">
                        </div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title"></div>
                        <div class="input-inline"><button class="flyer-button normal border-round" id="save-button" type="submit">保 存</button></div>
                    </div>
                    <textarea id="flyer-create-json" data-form="#flyer-create" style="display:none;"><?= Recruit::checker() ?></textarea>
                    <input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">
                </form>
            </div>
        </div>
    </div>
</div>

<script src="<?= Render::static('flyer/flyer.class.js') ?>"></script>
<script src="<?= Render::static('flyer/checker.class.js') ?>"></script>
<script src="<?= Render::static('flyer/tableHandler.class.js') ?>"></script>
<script>
    var checkerClass = new checker();
    $(document).ready(function() {
        // 类型选择
        $('input[name=type][value=<?= Render::value($recruit, 'type') ?>]').click();
        // 表单美化
        (new flyer).init({ form: '#flyer-create' });
        // 表单数据验证
        checkerClass.init({ ruleDom: '#flyer-create-json' });
        // 多选栏目选择数量控制
        var checkboxLimit = ['models'];
        $.each(checkboxLimit, function(index) {
            var name = checkboxLimit[index];
            var currentEvent = "input[name='" + name + "[]']";
            $(currentEvent).bind('change', function() {
                var checkboxs = "input[name='" + $(this).attr('name') + "']";
                if($(checkboxs + ':checked').length > 3) {
                    $(this).removeAttr('checked');
                    layer.msg('最多只能选择3个');
                    return false;
                }
                return true;
            });
        });
        var checkboxOne = ['styles'];
        $.each(checkboxOne, function(index) {
            var name = checkboxOne[index];
            var currentEvent = "input[name='" + name + "[]']";
            $(currentEvent).bind('change', function() {
                var checkboxs = "input[name='" + $(this).attr('name') + "']";
                if($(checkboxs + ':checked').length > 1) {
                    $(this).removeAttr('checked');
                    layer.msg('只能选择1个');
                    return false;
                }
                return true;
            });
        });

        // 保存按钮
        tableHandler.requestSingle({
            button: '#save-button', isConfirm: false, isShadow: false, isAlert: false,
            url: "<?= Url::to('@web/employer/recruit?id='.Render::value($recruit, 'id')) ?>",
            beforeRequest: function(param) {
                if( ! checkerClass.validate()) {
                    layer.msg('请先填写完整招聘信息');
                    return false;
                }
                param.data = $('#flyer-create').serialize();
                $('#save-button').attr('disabled', true);
            },
            requestFail: function(param) {
                $('#save-button').removeAttr('disabled');
            },
            requestSuccess: function (param) {
                $('#save-button').removeAttr('disabled');
                layer.alert(param.response.message, { icon: 6 }, function() {
                    window.location.href = "<?= Url::to('@web/employer/recruit-list') ?>";
                });
            }
        });
    });
</script>